import apiClient from "./client";
import type {
  UserDailyChallenge,
  Achievement,
  UserNotification,
} from "../types/community";
import type { DailyChallenge, SocialEnergyContent } from "../types/training";
import type { UserTier } from "../types/user";
import type { ApiResponse, PaginatedResponse } from "../types/api";

export const motivationApi = {
  // 获取今日挑战
  getTodayChallenge: (): Promise<DailyChallenge> =>
    apiClient.get("/daily-challenges/today"),

  // 完成今日挑战
  completeTodayChallenge: (
    challengeId: number
  ): Promise<ApiResponse<{ pointsEarned: number }>> =>
    apiClient.post(`/daily-challenges/${challengeId}/complete`),

  // 获取每日挑战
  getDailyChallenges: (date?: string): Promise<DailyChallenge[]> =>
    apiClient.get("/daily-challenges", { params: { date } }),

  // 获取用户每日挑战状态
  getUserDailyChallenges: (
    userId: number,
    date?: string
  ): Promise<UserDailyChallenge[]> =>
    apiClient.get(`/users/${userId}/daily-challenges`, { params: { date } }),

  // 完成每日挑战
  completeDailyChallenge: (
    challengeId: number
  ): Promise<ApiResponse<{ rewardEarned: number }>> =>
    apiClient.post(`/daily-challenges/${challengeId}/complete`),

  // 获取挑战历史
  getChallengeHistory: (userId: number): Promise<UserDailyChallenge[]> =>
    apiClient.get(`/daily-challenges/users/${userId}/history`),

  // 获取所有成就
  getAllAchievements: (): Promise<Achievement[]> =>
    apiClient.get("/achievements"),

  // 获取用户成就
  getUserAchievements: (userId: number): Promise<Achievement[]> =>
    apiClient.get(`/users/${userId}/achievements`),

  // 获取社交能量站内容
  getSocialEnergyContent: (): Promise<SocialEnergyContent[]> =>
    apiClient.get("/social-energy"),

  // 获取个性化鼓励内容
  getPersonalizedEncouragement: (
    userId: number
  ): Promise<SocialEnergyContent> =>
    apiClient.get(`/users/${userId}/encouragement`),

  // 获取用户段位信息
  getUserTier: (userId: number): Promise<UserTier> =>
    apiClient.get(`/users/${userId}/tier`),

  // 获取段位历史
  getTierHistory: (userId: number): Promise<UserTier[]> =>
    apiClient.get(`/users/${userId}/tier/history`),

  // 领取成就奖励
  claimAchievementReward: (
    achievementId: number
  ): Promise<ApiResponse<{ rewards: string[] }>> =>
    apiClient.post(`/achievements/${achievementId}/claim`),

  // 分享成就
  shareAchievement: (
    achievementId: number,
    platform?: string
  ): Promise<ApiResponse> =>
    apiClient.post(`/achievements/${achievementId}/share`, {
      platform,
    }),

  // 获取用户通知
  getNotifications: (
    userId: number,
    params?: { type?: string; isRead?: boolean; page?: number }
  ): Promise<PaginatedResponse<UserNotification>> =>
    apiClient.get(`/notifications/users/${userId}`, { params }),

  // 标记通知为已读
  markNotificationAsRead: (notificationId: number): Promise<ApiResponse> =>
    apiClient.put(`/notifications/${notificationId}/read`),

  // 标记所有通知为已读
  markAllNotificationsAsRead: (userId: number): Promise<ApiResponse> =>
    apiClient.put(`/notifications/users/${userId}/read-all`),

  // 订阅推送通知
  subscribePushNotification: (
    subscription: PushSubscription
  ): Promise<ApiResponse> =>
    apiClient.post("/notifications/subscribe", { subscription }),

  // 取消推送通知订阅
  unsubscribePushNotification: (): Promise<ApiResponse> =>
    apiClient.post("/notifications/unsubscribe"),

  // 获取用户统计数据
  getUserMotivationStats: (
    userId: number
  ): Promise<
    ApiResponse<{
      consecutiveDays: number;
      totalChallengesCompleted: number;
      totalAchievementsUnlocked: number;
      currentStreak: number;
      energyLevel: number;
    }>
  > => apiClient.get(`/users/${userId}/motivation-stats`),
};
